//
//  GraphicsVC2.m
//  test02
//
//  Created by lzh on 2020/10/21.
//  Copyright © 2020 NO. All rights reserved.
//

#import "GraphicsVC2.h"
#import "CustomView.h"
@interface GraphicsVC2 ()

@property(nonatomic,strong)CAShapeLayer * myLayer;
@end

@implementation GraphicsVC2

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];

    UIBezierPath *smoothedPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.view.center.x, 150) radius:80 startAngle:0 endAngle: 2 * M_PI clockwise:YES];
       CAShapeLayer *slice = [CAShapeLayer layer];
       slice.contentsScale = [[UIScreen mainScreen] scale];
       slice.fillColor = [UIColor clearColor].CGColor;
       slice.strokeColor = [UIColor blueColor].CGColor;
       slice.lineWidth = 2.0;
       slice.lineCap = kCALineJoinBevel;//线条拐角
       slice.lineJoin = kCALineJoinBevel;//终点处理
       slice.path = smoothedPath.CGPath;
//    slice.strokeEnd = 0; //strokeEnd 是从0-1 默认为1 就是最终的弧线 0的话没有弧线  下面加动画调试这个值了 所以这里设置没有意义
    
      CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
       pathAnimation.duration = 1.5;
       pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
       pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
       pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
       pathAnimation.autoreverses = NO;
       pathAnimation.repeatCount = MAXFLOAT;
    
       [slice addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
    
    self.myLayer = slice;
    [self.view.layer addSublayer:slice];
    
    
    CustomView * custom_view = [[CustomView alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];

    [self.view addSubview:custom_view];
    
    
    UIImageView * imagev = [[UIImageView alloc]initWithFrame:CGRectMake(100, 300+150, 100, 100)];
    
    imagev.image = [self makeCorner:imagev.bounds];
    [self.view addSubview:imagev];
    
    
}
-(UIImage *)makeCorner:(CGRect)rect{
      //创建图片上下文
      UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
     //获取栈顶上下文及 图片上下文
        CGContextRef context = UIGraphicsGetCurrentContext();
    //设置边框颜色
        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    //设置填充颜色
        CGContextSetFillColorWithColor(context, [UIColor purpleColor].CGColor);
    //边框的宽度
        CGContextSetLineWidth(context, 1.0);
    //贝瑟尔路径
        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(40, 40)];
    //添加路径
        CGContextAddPath(context,path.CGPath);
    //根据路径 画
        CGContextDrawPath(context, kCGPathFillStroke);
        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
    
    return image;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    self.myLayer.strokeColor = [UIColor redColor].CGColor;
}


@end
